﻿using System;

namespace Borg.Logging
{
    internal sealed class ConsoleLogger : TextLoggerBase
    {
        public override void Init(Runtime runtime)
        {
            base.Init(runtime);
            Log(new LogArgs(LogType.Info, null, "{0}", new object[] { Runtime.VersionInfo }));
        }

        public override void Log(LogArgs args)
        {
            var foregroundColor = Console.ForegroundColor;

            try
            {
                var extension = args.Source as Extension;

                if (extension != null)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write("[{0}:{1}]: ", extension.Id, extension.Name);
                    Console.ForegroundColor = foregroundColor;
                }

                if (args.Type != LogType.Info)
                    Console.ForegroundColor = args.Type == LogType.Error
                        ? ConsoleColor.Red
                        : ConsoleColor.Yellow;

                Console.WriteLine(args.Message);
            }
            finally
            {
                Console.ForegroundColor = foregroundColor;
            }
        }
    }
}
